//
//  SLTabBarController.h
//  SLTabBarVc+NavigationVc
//
//  Created by 伟仔c on 15/3/2.
//  Copyright (c) 2015年 SL. All rights reserved.
//

#import "SLTabBarController.h"
#import "SLNavigationController.h"
#import "SLTabBar.h"

@interface SLTabBarController () <SLTabBarDelegate>

@end

@implementation SLTabBarController

#pragma mark -第一次使用这个类
+ (void)initialize {
    // 1.设置导航条的字体样式
    UITabBarItem *tabBarItem = [UITabBarItem appearance];
    
    // 1.1 普通状态
    NSMutableDictionary *itemDict = [NSMutableDictionary dictionary];
    itemDict[NSFontAttributeName] = [UIFont systemFontOfSize:15];   // 字体大小
    [tabBarItem setTitleTextAttributes:itemDict forState:UIControlStateNormal];
    
    // 1.2 高亮状态
    NSMutableDictionary *itemHighDict = [NSMutableDictionary dictionary];
    itemHighDict[NSForegroundColorAttributeName] = [UIColor orangeColor]; // 字体颜色
    [tabBarItem setTitleTextAttributes:itemHighDict forState:UIControlStateSelected];
}

#pragma mark -实例化一个应用程序的骨架
+ (instancetype)tabBarViewControllerWithTitles:(NSArray *)Titles images:(NSArray *)images selectedImages:(NSArray *)selectedImages controllers:(NSArray *)controllers {
    
    // 1.实例化自己
    SLTabBarController *tabBarVc = [[self alloc] init];
    
    // 2.存储标题
    tabBarVc.titles = Titles;
    
    // 3.存储Item图片
    tabBarVc.images = images;
    
    // 4.存储Item选中时图片
    tabBarVc.selectedImages = selectedImages;
    
    // 5.存储所有控制器
    tabBarVc.controllers = controllers;
    
    // 6.返回自己
    return tabBarVc;
}

#pragma mark -视图即将显示
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // 1.添加控制器
    [self addAllChildController];
    
    // 2.检查是否自定义TabBar
    if (_titles.count == 0) {
        [self replaceTabBarAction];
    }
}

#pragma mark -视图显示完毕
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    // 1.检查是否自定义TabBar
    if (_titles.count == 0) {
        // 1.1 删除原来的TabBar按钮
        for (UIView *view in self.tabBar.subviews) {
            if (![view isKindOfClass:[SLTabBar class]]) [view removeFromSuperview];
        }
    }
}

#pragma mark -添加所有子控制器
- (void)addAllChildController {
    
    // 1.遍历所有控制器
    for (int index = 0; index < _controllers.count; index++) {
        UIViewController *vc = _controllers[index];
        NSString *title = _titles[index];
        NSString *image = _images[index];
        NSString *selectedImage = _selectedImages[index];
        
        // 2.添加到父控制器
        [self addChildVc:vc title:title imageName:image selectorImageName:selectedImage];
    }
}

#pragma mark -快速添加一个子控制器
- (void)addChildVc:(UIViewController *)Vc title:(NSString *)title imageName:(NSString *)imageName selectorImageName:(NSString *)selectorImageName {
    
    // 0.设置背景颜色
    Vc.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0];
    
    // 1.设置标题
    Vc.title = title;
    
    // 2.设置图片
    Vc.tabBarItem.image = [UIImage imageNamed:imageName];
    
    // 3.设置选中图片
    Vc.tabBarItem.selectedImage = [UIImage imageNamed:selectorImageName];
    
    // 4.创建导航控制器
    SLNavigationController *nav = [[SLNavigationController alloc] initWithRootViewController:Vc];
    
    // 5.添加到父控制器
    [self addChildViewController:nav];
}

#pragma mark -自定义TabBar
- (void)replaceTabBarAction {
    
    // 1.创建自定义TabBar
    SLTabBar *tabBar = [[SLTabBar alloc] init];
    
    // 2.设置自定义TabBar属性
    tabBar.delegate = self;
    tabBar.frame = self.tabBar.bounds;
    
    // 3.添加到父视图
    [self.tabBar addSubview:tabBar];
    
    // 4.创建自定义TabBar的按钮
    NSString *image = nil;
    NSString *selectImage = nil;
    for (int i = 0; i < _controllers.count; i++) {
        image = _images[i];
        selectImage = _selectedImages[i];
        // 4.1 快速创建一个TabBar按钮
        [tabBar addTabBarButtonWithImage:image SelectImage:selectImage];
    }
}

#pragma mark -自定义TabBar代理方法，切换选中的控制器
- (void)tabBar:(SLTabBar *)tabBar didSelectIndex:(NSInteger)selectIndex {
    self.selectedIndex = selectIndex;
}

@end
